//
//  WaveformView.swift
//  WaveformDemo
//
//  Created by liyahao on 2025/4/11.
//

import SwiftUI

// 指示指针
struct TimeIndicator: View {
    let color: Color = .yellow
    let lineWidth: CGFloat = 1

    var body: some View {
        GeometryReader { geometry in
            let midX = geometry.size.width / 2
            ZStack {
                Path { path in
                    path.move(to: CGPoint(x: midX, y: 0))
                    path.addLine(to: CGPoint(x: midX, y: geometry.size.height - 10))
                }
                .stroke(color, lineWidth: lineWidth)
                Circle().fill(color).frame(width: 5, height: 5).position(x: midX, y: 0)
                Circle().fill(color).frame(width: 5, height: 5).position(x: midX, y: geometry.size.height - 10)
            }
        }
    }
}

struct WaveformView: View {
    @Binding var samples: [CGFloat]
    let viewWidth: CGFloat
    // 采样率
    var sampleRate: TimeInterval
    
    var barWidth: CGFloat = 2
    var spacing: CGFloat = 2
    var color: Color = .red
    var tickHeight: CGFloat = 5
    
    var body: some View {
        // 总宽度
        let requiredCanvasWidth = CGFloat(samples.count) * (barWidth + spacing) + viewWidth
        
        // 画时间：step = 1/sampleRate * (barWidth + spacing)计算1秒跨度
        let step: CGFloat = 1 / sampleRate * (barWidth + spacing)
        let duration: TimeInterval = Double(samples.count) * 0.05
        let maxSeconds = Int(ceil(duration + (viewWidth / step)))
        
        Canvas { context, size in
            // 声音最大高度
            let maxHeight = size.height - 20
            let barPlusSpacing = barWidth + spacing
            // 设置起始位置为viewWidth一半，从中间开始
            context.translateBy(x: viewWidth / 2, y: 0) // Apply base offset

            // 画出音量
            for (index, sample) in samples.enumerated() {
                let barHeight = max(1, sample.magnitude * maxHeight)
                let xPos = CGFloat(index) * barPlusSpacing
                let yPos = (maxHeight - barHeight) / 2 // Center vertically
                let rect = CGRect(x: xPos, y: yPos, width: barWidth, height: barHeight)
                context.fill(Path(rect), with: .color(color))
            }
            
            // 画刻度
            for second in 0...maxSeconds {
                // 起始点
                let xPos = CGFloat(second) * step
                // 过滤范围之外，只画范围内的线
                guard xPos >= 0 && xPos <= requiredCanvasWidth else { continue }
                
                // 终点
                let yPos = size.height - tickHeight
                let tickPath = Path { p in
                    p.move(to: CGPoint(x: xPos, y: yPos))
                    p.addLine(to: CGPoint(x: xPos, y: size.height))
                }
                context.stroke(tickPath, with: .color(.gray.opacity(0.7)), lineWidth: 1)

                // 画文字
                let timeText = Text(second.formatTime).font(.caption).foregroundColor(.gray)
                let point = CGPoint(x: xPos, y: size.height - tickHeight - 8)
                context.draw(timeText, at: point, anchor: .center)
            }
        }
        .frame(width: requiredCanvasWidth)
        .id("canvasContent")
    }
}

//#Preview {
//    WaveformView()
//}
